package com.jyb.qiyefuwu.view.adpater;

import android.content.Context;
import android.graphics.Paint;
import android.text.TextUtils;
import android.view.View;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.jyb.qiyefuwu.BR;
import com.jyb.qiyefuwu.R;
import com.jyb.qiyefuwu.databinding.ItemSaleorderBinding;
import com.jyb.qiyefuwu.entity.MakeOverOrderEntity;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.ArrayList;

public class MakeOverOrderAdpter extends BaseRecycleAdapter<MakeOverOrderEntity> {

    /**销售订单待接单关闭订单***/
    public static final int CLICK_SALE_COLOSEORDER = 0x115;
    /**销售订单待接单确认接单**/
    public static final int CLICK_SALE_CONFRIMORDER = 0x116;
    /**采购订单待接单关闭订单**/
    public static final int CLICK_PURCHASE_COLOSEORDER = 0x117;
    /**已关闭删除订单**/
    public static final int CLICK_DELETEORDER = 0x118;
    /**采购订单待支付取消订单***/
    public static final int CLICK_PURCHASE_PAYOFF_COLOSEORDER = 0x119;
    /**采购订单待支付立即支付***/
    public static final int CLICK_PURCHASE_PAYOFF_GOTOPAY = 0x120;
    /**采购订单交易中确认完成**/
    public static final int CLICK_PURCHASE_CONFIRM_SERSS = 0x121;
    /***销售订单交易中提交凭证**/
    public static final int CLICK_SALEORDER_SUBMIT_DATA = 0x122;


    private int buyerOrSeller = 0;//0销售订单 1采购订单
    RequestOptions teamOptions = new RequestOptions().placeholder(R.mipmap.image_order_list_defuntimage)//图片加载出来前，显示的图片
            .fallback(R.mipmap.image_order_list_defuntimage) //url为空的时候,显示的图片
            .error(R.mipmap.image_order_list_defuntimage)//图片加载失败后，显示的图片
           ;
    // .transforms(new CenterCrop(), new RoundedCorners(2))

    public MakeOverOrderAdpter(Context context) {
        super(context, R.layout.item_saleorder, new ArrayList<>());
    }

    public void setBuyerOrSeller(int buyerOrSeller) {
        this.buyerOrSeller = buyerOrSeller;
        notifyDataSetChanged();
    }

    @Override
    public void convert(ItemViewHolder holder, MakeOverOrderEntity makeOverOrderEntity) {
        if (holder.getmBinding() instanceof ItemSaleorderBinding) {
            holder.setBinding(BR.model, makeOverOrderEntity);

            ((ItemSaleorderBinding) holder.getmBinding()).tvOrderStatus.setText(
                    makeOverOrderEntity.getOrderStatus() == 1 ? "待接单" :
                            makeOverOrderEntity.getOrderStatus() == 2 ? "待付款" :
                                    makeOverOrderEntity.getOrderStatus() == 3 ? "已关闭" :
                                            makeOverOrderEntity.getOrderStatus() == 4 ? "交易中" :
                                                    makeOverOrderEntity.getOrderStatus() == 5 ? "已完成" : ""
            );

            ((ItemSaleorderBinding) holder.getmBinding()).tvOrderTitle.setText(buyerOrSeller == 0 ? "买方：" + makeOverOrderEntity.getBuyersName() : "卖方：" + makeOverOrderEntity.getSellerName());
            switch (makeOverOrderEntity.getOrderType()) {
                case 1://商标
                    ((ItemSaleorderBinding) holder.getmBinding()).tvName1.setText("商标名称：" + (TextUtils.isEmpty(makeOverOrderEntity.getTradeMarkName()) ? "/" : makeOverOrderEntity.getTradeMarkName()));
                    ((ItemSaleorderBinding) holder.getmBinding()).tvName2.setText("商标分类：" + (TextUtils.isEmpty(makeOverOrderEntity.getTrademarkClassification()) ? "/" : makeOverOrderEntity.getTrademarkClassification()));
                    ((ItemSaleorderBinding) holder.getmBinding()).tvName3.setText("商标注册号：" + (TextUtils.isEmpty(makeOverOrderEntity.getTradeMarkRegistration()) ? "/" : makeOverOrderEntity.getTradeMarkRegistration()));
                    Glide.with(context).load(makeOverOrderEntity.getTrademarkImg()).apply(teamOptions).into(((ItemSaleorderBinding) holder.getmBinding()).imageGoodsIcon);
                    break;
                case 2://专利
                    ((ItemSaleorderBinding) holder.getmBinding()).tvName1.setText("专利名称：" + (TextUtils.isEmpty(makeOverOrderEntity.getPatentName()) ? "/" : makeOverOrderEntity.getPatentName()));
                    ((ItemSaleorderBinding) holder.getmBinding()).tvName2.setText("专利类型：" + (TextUtils.isEmpty(makeOverOrderEntity.getPatentClassification()) ? "/" : makeOverOrderEntity.getPatentClassification()));
                    ((ItemSaleorderBinding) holder.getmBinding()).tvName3.setText("专利注册号：" + (TextUtils.isEmpty(makeOverOrderEntity.getPatentRegistration()) ? "/" : makeOverOrderEntity.getPatentRegistration()));
                    if(!TextUtils.isEmpty(makeOverOrderEntity.getPatentImg())) {
                        Glide.with(context).load(makeOverOrderEntity.getPatentImg()).apply(teamOptions).into(((ItemSaleorderBinding) holder.getmBinding()).imageGoodsIcon);
                    }else if(!TextUtils.isEmpty(makeOverOrderEntity.getPatentClassification())){
                        switch (makeOverOrderEntity.getPatentClassification()) {
                            case "发明专利":
                                Glide.with(context).load(R.mipmap.qiyefuwu_module_famingzhuanli_for_patent_publish).into( ((ItemSaleorderBinding) holder.getmBinding()).imageGoodsIcon);
                                break;
                            case "实用新型":
                                Glide.with(context).load(R.mipmap.qiyefuwu_module_shiyongxinxing_patent_publish).into(((ItemSaleorderBinding) holder.getmBinding()).imageGoodsIcon);
                                break;
                            case "外观设计":
                                Glide.with(context).load(R.mipmap.qiyefuwu_module_waiguansheji_patent_publish).into(((ItemSaleorderBinding) holder.getmBinding()).imageGoodsIcon);
                                break;
                        }
                    }
                    break;
                case 3://版权
                        ((ItemSaleorderBinding) holder.getmBinding()).tvName1.setText("版权名称：" + (TextUtils.isEmpty(makeOverOrderEntity.getCopyrightName()) ? "/" : makeOverOrderEntity.getCopyrightName()));
                    ((ItemSaleorderBinding) holder.getmBinding()).tvName2.setText("版权分类：" + (TextUtils.isEmpty(makeOverOrderEntity.getClassificationCopyright()) ? "/" : makeOverOrderEntity.getClassificationCopyright()));
                    ((ItemSaleorderBinding) holder.getmBinding()).tvName3.setText("登记号：" + (TextUtils.isEmpty(makeOverOrderEntity.getCopyrightNumber()) ? "/" : makeOverOrderEntity.getCopyrightNumber()));
                    Glide.with(context).load(makeOverOrderEntity.getCopyrightImg()).apply(teamOptions).into(((ItemSaleorderBinding) holder.getmBinding()).imageGoodsIcon);
                    break;
            }

            ((ItemSaleorderBinding) holder.getmBinding()).tvBargainingPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG); // 设置中划线并加清晰
            ((ItemSaleorderBinding) holder.getmBinding()).tvUserJb.setText(buyerOrSeller == 0 ? (TextUtils.isEmpty(makeOverOrderEntity.getBuyerAgent()) ? "经办人：" + "" : "经办人：" + makeOverOrderEntity.getBuyerAgent()) : (TextUtils.isEmpty(makeOverOrderEntity.getAgent()) ? "经办人：" + "" : "经办人：" + makeOverOrderEntity.getAgent()));
            ((ItemSaleorderBinding) holder.getmBinding()).tvSaleColoseorder.setVisibility(View.GONE);
            ((ItemSaleorderBinding) holder.getmBinding()).tvSaleConfirmorder.setVisibility(View.GONE);
            ((ItemSaleorderBinding) holder.getmBinding()).tvColoseorder.setVisibility(View.GONE);
            ((ItemSaleorderBinding) holder.getmBinding()).tvDeleteorder.setVisibility(View.GONE);
            ((ItemSaleorderBinding) holder.getmBinding()).tvPayoffColoseorder.setVisibility(View.GONE);
            ((ItemSaleorderBinding) holder.getmBinding()).tvPayoffConfirmorder.setVisibility(View.GONE);
            ((ItemSaleorderBinding) holder.getmBinding()).tvSaleConfirmsecess.setVisibility(View.GONE);
            ((ItemSaleorderBinding) holder.getmBinding()).tvSaleSubmitpinz.setVisibility(View.GONE);
            switch (makeOverOrderEntity.getOrderStatus()) {
                case 1://待接单
                    ((ItemSaleorderBinding) holder.getmBinding()).tvSaleColoseorder.setVisibility(buyerOrSeller == 0 ? View.VISIBLE : View.GONE);
                    ((ItemSaleorderBinding) holder.getmBinding()).tvSaleConfirmorder.setVisibility(buyerOrSeller == 0 ? View.VISIBLE : View.GONE);
                    ((ItemSaleorderBinding) holder.getmBinding()).tvColoseorder.setVisibility(buyerOrSeller == 1 ? View.VISIBLE : View.GONE);
                    break;
                case 2://待支付
                    ((ItemSaleorderBinding) holder.getmBinding()).tvPayoffColoseorder.setVisibility(buyerOrSeller == 1 ?View.VISIBLE:View.GONE);
                    ((ItemSaleorderBinding) holder.getmBinding()).tvPayoffConfirmorder.setVisibility(buyerOrSeller == 1 ?View.VISIBLE:View.GONE);
                    if(((ItemSaleorderBinding) holder.getmBinding()).tvPayoffConfirmorder.getVisibility()==View.VISIBLE){
                        ((ItemSaleorderBinding) holder.getmBinding()).tvPayoffConfirmorder.setText(SharedPreferenceUtil.getInstance().getSaveIntData(SharedPreferenceUtil.COM_TYPE, 0) == 1?"申请付款":"立即支付");
                    }
                    break;
                case 3://已关闭
                    ((ItemSaleorderBinding) holder.getmBinding()).tvDeleteorder.setVisibility(View.VISIBLE);
                    break;
                case 4://交易中
                    if(buyerOrSeller==0){//销售订单
                        ((ItemSaleorderBinding) holder.getmBinding()).tvSaleSubmitpinz.setVisibility(makeOverOrderEntity.getProcessStatus()!=4?View.VISIBLE:View.GONE);
                    }else{//采购订单
                        ((ItemSaleorderBinding) holder.getmBinding()).tvSaleConfirmsecess.setVisibility(makeOverOrderEntity.getProcessStatus()==4?View.VISIBLE:View.GONE);
                    }
                    break;
                case 5://已完成


                    break;
            }

            ((ItemSaleorderBinding) holder.getmBinding()).tvSaleColoseorder.setOnClickListener(view ->{
                if(onItemClick!=null){
                    onItemClick.onItemClick(makeOverOrderEntity,CLICK_SALE_COLOSEORDER);
                }
            });
            ((ItemSaleorderBinding) holder.getmBinding()).tvSaleConfirmorder.setOnClickListener(view ->{
                if(onItemClick!=null){
                    onItemClick.onItemClick(makeOverOrderEntity,CLICK_SALE_CONFRIMORDER);
                }
            });
            ((ItemSaleorderBinding) holder.getmBinding()).tvColoseorder.setOnClickListener(view ->{
                if(onItemClick!=null){
                    onItemClick.onItemClick(makeOverOrderEntity,CLICK_PURCHASE_COLOSEORDER);
                }
            });
            ((ItemSaleorderBinding) holder.getmBinding()).tvDeleteorder.setOnClickListener(view ->{
                if(onItemClick!=null){
                    onItemClick.onItemClick(makeOverOrderEntity,CLICK_DELETEORDER);
                }
            });
            ((ItemSaleorderBinding) holder.getmBinding()).tvPayoffColoseorder.setOnClickListener(view ->{
                if(onItemClick!=null){
                    onItemClick.onItemClick(makeOverOrderEntity,CLICK_PURCHASE_PAYOFF_COLOSEORDER);
                }
            });
            ((ItemSaleorderBinding) holder.getmBinding()).tvPayoffConfirmorder.setOnClickListener(view ->{
                if(onItemClick!=null){
                    onItemClick.onItemClick(makeOverOrderEntity,CLICK_PURCHASE_PAYOFF_GOTOPAY);
                }
            });

            ((ItemSaleorderBinding) holder.getmBinding()).tvSaleSubmitpinz.setOnClickListener(view -> {
                if(onItemClick!=null){
                    onItemClick.onItemClick(makeOverOrderEntity,CLICK_SALEORDER_SUBMIT_DATA);
                }
            });
            ((ItemSaleorderBinding) holder.getmBinding()).tvSaleConfirmsecess.setOnClickListener(view -> {
                if(onItemClick!=null){
                    onItemClick.onItemClick(makeOverOrderEntity,CLICK_PURCHASE_CONFIRM_SERSS);
                }
            });
        }

    }


    public void removeData(long orderid){
        for (int i = datas.size() - 1; i >= 0; i--) {
            if(datas.get(i).getId()==orderid){

//                removeItem(i);
//                notifyItemRangeChanged(0,getItemCount());
                datas.remove(i);
                notifyDataSetChanged();
            }
        }
    }
}
